<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2019/2/23
 * Time: 19:58
 */

namespace app\common\service;


use app\common\helper\RequestHelper;
use app\common\helper\UserHelper;
use app\common\model\Admin;
use app\common\utils\MyCrypt;

class AdminService
{
    public static function getAdminById($adminId){
        $model = new Admin();
        return $model->get($adminId);
    }

    /**
     * 检查用户名密码
     * @param $userName
     * @param $pass
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function checkUserNameAndPass($userName, $pass)
    {
        $model = new Admin();
        $obj = $model->where(['username'=>$userName,'pass'=>$pass])->find();
        if($obj){
            return $obj->id;
        }
        return false;
    }

    /**
     * 添加用户
     * @param $username string 用户名
     * @param $password string 密码
     * @param $mobile string 手机号
     * @param $type int 账号类型
     * @param $realname string 真实姓名
     * @param $channelId int 渠道id
     * @return bool
     */
    public static function addAdmin($username, $password, $mobile, $type, $realname, $channelId =0)
    {
        $model = new Admin();
        try {
            $last = $model->save([
                'username' => $username,
                'pass' => UserHelper::makePassword($password),
                'tel' => $mobile,
                'type' => $type,
                'realname' => $realname,
                'channel_id' => $channelId,
                'ip' => RequestHelper::getClientIp(),
            ]);
            return true;
        } catch (\Exception $e) {
            var_dump($model->getLastSql());
            return false;
        }
    }

    /**
     * 通过渠道号 查找用户id
     * @param $channelId
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAdminByChannelId($channelId)
    {
        $model = new Admin();
        $obj = $model->where(['channel_id'=>$channelId])->find();
        return $obj;
    }
}